package com.platform.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class EmailUtil {

    @Value("${mail.fromMail.sender}")
    private String sender;

    @Autowired
    private JavaMailSender mailSender;

    //sender 发送人 recever接收人 subject主题 text内容
    public String sendMail(String receiver, String subject, String text) {

        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(sender);
            message.setTo(receiver);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
            log.info("邮件已经发送。");
        } catch (Exception e) {
            log.error("发送简单邮件时发生异常！");
            e.printStackTrace();
        }
        return "success";
    }
}
